package com.xsmart.oa.services.mall.mymenu;

import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.mall.mymenu.UserApplyDao;
import com.xsmart.common.entity.user.SellerShopInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UserApplyService {

    @Autowired
    UserApplyDao userApplyDao;

    /**
     * 分销商申请
     * @param requestBean
     * @return
     */
    public ResponseBean addTask(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("userId");
        Integer taskType = requestBean.getRequestParamInteger("taskType");
        int num = userApplyDao.addTask(userId,taskType);
        ResponseBean responseBean = requestBean.getResponseBean();
        if(num > 0){
            responseBean.setResult(ResultConstant.SUCCESS);
        }else{
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 查看会员的申请情况
     * @param requestBean
     * @return
     */
    public ResponseBean queryTaskByUserId(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("userId");
        List<Map<String,Object>> taskList = userApplyDao.queryTaskByUserId(userId);
        ResponseBean responseBean = requestBean.getResponseBean();
        if(taskList!= null){
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("taskList",taskList);
        }else{
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }


    /**
     * 供应商申请
     * @param requestBean
     * @return
     */
    @Transactional
    public ResponseBean supplierApply(RequestBean requestBean) {
        SellerShopInfo shopInfo = new SellerShopInfo();
        Integer province = null;
        Integer city = null;
        Integer district = null;
        Integer userId = requestBean.getRequestParamInteger("userId");
        Integer taskType = requestBean.getRequestParamInteger("taskType");
        String address = requestBean.getRequestParamString("address");
        String addr1 = requestBean.getRequestParamString("addr");
        System.out.println(addr1);
        String kfTel = requestBean.getRequestParamString("kfTel");
        String kfqq = requestBean.getRequestParamString("kfqq");
        String kfww = requestBean.getRequestParamString("kfww");
//        char mobile = (char) requestBean.getRequestParam("mobile");
        String mobile = requestBean.getRequestParamString("mobile");
        mobile = mobile.replace(" ","");
        String notice = requestBean.getRequestParamString("notice");
        String sellerEmail = requestBean.getRequestParamString("sellerEmail");
        String shopKeyword = requestBean.getRequestParamString("shopKeyword");
        String shopName = requestBean.getRequestParamString("shopName");
        String shopTitle = requestBean.getRequestParamString("shopTitle");
        Integer status = requestBean.getRequestParamInteger("status");
        List<Integer> addr = (List<Integer>) requestBean.getRequestParam("addr");
        if (addr.size()>0){
            province = addr.get(0);
        }
        if (addr.size()>1){
            city = addr.get(1);
        }
        if (addr.size()>2){
            district = addr.get(2);
        }
        shopInfo.setRuId(userId);
        shopInfo.setShopName(shopName);
        shopInfo.setShopTitle(shopTitle);
        shopInfo.setShopKeyword(shopKeyword);
        shopInfo.setShopAddress(address);
        shopInfo.setSellerEmail(sellerEmail);
        shopInfo.setKfqq(kfqq);
        shopInfo.setKfww(kfww);
        shopInfo.setKfTel(kfTel);
        shopInfo.setMobile(mobile);
        shopInfo.setNotice(notice);
        shopInfo.setStatus(status);
        shopInfo.setCountry(1);
        shopInfo.setProvince(province);
        shopInfo.setCity(city);
        shopInfo.setDistrict(district);

        ResponseBean responseBean = requestBean.getResponseBean();

        try {
            userApplyDao.addTask(userId,taskType);
            userApplyDao.addSellerShop(shopInfo);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }


}
